package cn.takujo.takujoframework.common.util.code_generator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 数据库处理器（暂只能处理mysql数据库）
 * 
 * @author wzx
 *
 */
public class DatabaseHandler {

	private DbUtil du = new DbUtil();
	private String database;

	public DatabaseHandler(String database) {
		this.database = database;
	}

	public static Class<?> javaType(String mysqlType) {
		if (mysqlType.contains("varchar") || mysqlType.contains("VARCHAR")) {
			return String.class;
		} else if (mysqlType.contains("int") || mysqlType.contains("INT")) {
			return Integer.class;
		} else if (mysqlType.contains("double") || mysqlType.contains("DOUBLE")) {
			return Double.class;
		} else if (mysqlType.contains("datetime") || mysqlType.contains("date")) {
			return Date.class;
		} else if (mysqlType.contains("DATETIME") || mysqlType.contains("DATE")) {
			return Date.class;
		} else if (mysqlType.contains("text") || mysqlType.contains("TEXT")) {
			return String.class;
		} else {
			return String.class;
		}
	}

	public List<String> findTableList() {
		String sql = "select table_name from information_schema.tables where table_schema=?";
		List<Map<String, Object>> list = du.exeQuerySql(sql, new Object[] { database });
		List<String> res = new ArrayList<>();
		for (Map<String, Object> map : list) {
			res.add((String) map.get("table_name"));
		}
		return res;
	}

	public List<Map<String, Object>> findFieldList(String table) {
		String sql = "select column_name,column_type from information_schema.columns "
				+ "where table_schema=? and table_name=?";
		return du.exeQuerySql(sql, new Object[] { database, table });
	}

}
